// Composables
import {createRouter, createWebHistory, type RouteRecordRaw} from 'vue-router'

const Layout = () => import("@/layouts/index.vue")
const ItemView = () => import("@/components/ItemView.vue")
// const Layout = () => import("@/layouts/default/Default.vue")


// 获取当前页面ip
const domain = window.location.host


export const constantRoutes: RouteRecordRaw[] = [

  {
    path: "/redirect",
    component: Layout,
    meta: {
      hidden: true
    },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/views/error/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/error/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: '/',
    component: Layout,
    name: 'Home',
    redirect: "/home",
    meta: {
      title: '首页'
    },
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('@/views/Home/index.vue'),
        meta: {
          title: '首页'
          , icon: 'mdi-home'
        }
      }

    ],
  },

  // 该页面仅用于测试，后续删除
  // 注意外链链接
  // 请在 meta 中添加 href 属性指向外部链接
  // 组件 component: ItemView,
  {
    path: '/test',
    redirect: "/test/page0",
    component: Layout,
    name: 'Test',
    meta: {
      title: '测试页面'
      , icon: 'mdi-emoticon-poop',
      hidden: true // 不显示该路由！！
    },
    children: [
      {
        path: 'page0',
        name: 'TestPage0',
        component: () => import('@/views/test/index.vue'),
        meta: {
          title: '测试内部网页'
          , icon: 'mdi-link-plus'

        }
      },
      {
        path: 'page1',
        name: 'TestPage1',
        component: ItemView,
        meta: {
          title: '抖音'
          , icon: 'mdi-link-plus',
          href: 'https://www.douyin.com'
        }
      },
      {
        path: 'page2',
        name: 'TestPage2',
        component: ItemView,
        meta: {
          title: '图标'
          , icon: 'mdi-link-plus',
          href: 'https://pictogrammers.com/library/mdi/'
        }
      },

      {
        path: 'page3',
        name: 'TestPage3',
        component: () => import('@/views/test/index2.vue'),
        meta: {
          title: '嵌入网页'
          , icon: 'mdi-emoticon-poop'
        }
      },
      {
        path: 'page4',
        name: 'TestPage4',
        component: () => import('@/views/test/index3.vue'),
        meta: {
          title: '嵌入图标'
          , icon: 'mdi-simple-icons'

        }
      },
    ]
  },
  {
    path: '/user',
    component: Layout,
    redirect: "/user/list",
    name: 'User',
    meta: {
      title: '用户管理',
      icon: 'mdi-account-cog'
    },
    children: [
      {
        path: 'list',
        name: 'UserList',
        component: () => import('@/views/User/index.vue'),
        meta: {
          title: '用户管理中心'
          , icon: 'mdi-account-cog',

        }
      },
    ],
  },


  {
    path: '/skin',
    component: ItemView,
    name: 'Skin',
    meta: {
      title: '皮肤',
      icon: 'mdi-cookie-edit'
    },
    children: [
      {
        path: 'skineditor',
        name: 'SkinEditor',
        component: () => import('@/views/skineditor/index.vue'),
        meta: {
          title: '皮肤编辑器'
          , icon: 'mdi-cookie-edit',
          href: `http://${domain}/skin/skineditor`
        }
      },
    ],
  },


  {
    path: '/account',
    component: Layout,
    redirect: "/account/main",
    name: 'Account',
    meta: {
      title: '账户',
      icon: 'mdi-account-cog'
    },
    children: [
      {
        path: 'main',
        name: 'Account1',
        component: () => import('@/views/Account/index.vue'),
        meta: {
          title: '账户'
          , icon: 'mdi-account'
        }
      },
      {
        path: 'favorite',
        name: 'favorite',
        component: () => import('@/views/Account/favorite/index.vue'),
        meta: {
          title: '收藏'
          , icon: 'mdi-star-box-multiple'
        }
      },
      {
        path: 'inventory',
        name: 'inventory',
        component: () => import('@/views/Account/inventory/index.vue'),
        meta: {
          title: '仓库'
          , icon: 'mdi-trophy-variant-outline'
        }
      },
    ],
  },
  {
    path: '/achievement',
    redirect: "/architectural/archves",
    name: 'Achievement',
    component: Layout,
    meta: {
      title: '成就'
      , icon: 'mdi-assistant'
    },
    children: [
      {
        path: 'archves',
        name: 'Archves',
        component: () => import('@/views/Achievement/index.vue'),
        meta: {
          title: '成就'
          , icon: 'mdi-assistant'
        }
      },
    ],
  },
  {
    path: '/workshop',
    redirect: "/workshop/index",
    name: 'Workshop',
    component: Layout,
    meta: {
      title: '工坊'
      , icon: 'mdi-hammer-screwdriver'
    },
    children: [
      {
        path: 'workshop',
        name: 'workshop',
        component: () => import('@/views/workshop/index.vue'),
        meta: {
          title: '工坊'
          , icon: 'mdi-hammer-wrench'
        }
      },
    ],
  },
  {
    path: '/redStone',
    redirect: "/redStoneArchives/index",
    component: Layout,
    name: 'redStone',
    meta: {
      title: '红石1'
      , icon: 'mdi-minecraft'
    },
    children: [
      {
        path: 'index',
        name: 'index',
        component: () => import('@/views/RedStoneArchives/index.vue'),
        meta: {
          title: '红石2'
          , icon: 'mdi-minecraft'
        }
      },
    ],
  },
  {
    path: '/architectural',
    redirect: "/architectural/archves",
    component: Layout,
    name: 'Architectural',
    meta: {
      title: '建筑成就'
      , icon: 'mdi-office-building-outline'
    },
    children: [
      {
        path: 'archves',
        name: 'RedArchive',
        component: () => import('@/views/ArchitecturalArchives/index.vue'),
        meta: {
          title: '建筑成就'
          , icon: 'mdi-office-building-outline'
        }
      },
    ],
  },

  {
    path: '/library',
    component: Layout,
    redirect: "/library/main",
    meta: {
      title: '图书馆'
      , icon: 'mdi-book-open-page-variant-outline'
    },
    children: [
      {
        path: 'main',
        name: 'Library',
        component: () => import('@/views/Library/index.vue'),
        meta: {
          title: '图书馆'
          , icon: 'mdi-book-open-page-variant-outline'
        }
      },
    ],
  },
  {
    path: '/market-house',
    redirect: "/market/house",
    name: 'Market',
    component: Layout,
    meta: {
      title: '市场'
      , icon: 'mdi-store'
    },
    children: [
      {
        path: 'house',
        name: 'MarketHouse',
        component: () => import('@/views/MarketHouse/index.vue'),
        meta: {
          title: '市场'
          , icon: 'mdi-store'
        }
      },
    ],
  },
  {
    path: '/message',
    redirect: "/message/notification",
    component: Layout,
    name: 'Message',
    meta: {
      title: '消息通知'
      , icon: 'mdi-bell'
    },
    children: [
      {
        path: 'notification',
        name: 'MessageNotification',
        component: () => import('@/views/MessageNotification/index.vue'),
        meta: {
          title: '消息通知'
          , icon: 'mdi-bell'
        }
      },
    ]
  },
  {
    path: '/outlink',
    component: Layout,
    name: 'Outlink',
    redirect: "/outlink/links",
    meta: {
      title: '项目外链',
      icon: 'mdi-link-plus',
    },
    children: [
      {
        path: 'links',
        name: 'OutLinks',
        component: () => import('@/views/OutLink/index.vue'),
        meta: {
          title: '项目外链'
          , icon: 'mdi-link-plus'
        }
      },
    ]

  },


]

export const asyncRoutes: RouteRecordRaw[] = [
  {
    path: "/permission",
    component: Layout,
    redirect: "/permission/page",
    name: "Permission",
    meta: {
      title: "权限管理",
      icon: 'mdi-shield-account',
      roles: ["admin", "editor"],
      hidden: false
    },
    children: [
      {
        path: "page",
        component: () => import("@/views/permission/index.vue"),
        name: "PagePermission",
        meta: {
          title: "页面权限",
          icon: 'mdi-security',
          roles: ["admin"]
        }
      },
      {
        path: "directive",
        component: () => import("@/views/permission/directive.vue"),
        name: "DirectivePermission",
        meta: {
          title: "指令权限",
          icon: 'mdi-shield-lock-outline'
          // 如果未设置角色，则表示：该页面不需要权限，但会继承根路由的角色
        }
      }
    ]
  },
  {
    path: "/:pathMatch(.*)*",
    redirect: "/404",
    name: "ErrorPage",
    meta: {
      hidden: true
    }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes: [...asyncRoutes, ...constantRoutes],
})

export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const {name, meta} = route
      if (name && (meta?.roles as string[]).length > 0) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch (error) {
    if (window?.location?.reload) {
      window.location.reload()
    }
  }
}


export default router
